package com.laijiale.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laijiale.Service.AssetsService;
import com.laijiale.dto.PageDTO;
import com.laijiale.dto.AssetsInsertDTO;
import com.laijiale.dto.AssetsPageDTO;
import com.laijiale.dto.AssetsUpdateDTO;
import com.laijiale.entities.Assets;
import com.laijiale.exception.ServiceException;
import com.laijiale.mapper.DeptMapper;
import com.laijiale.mapper.AssetsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 12:02
 */
@Service
@CacheConfig(cacheNames = "assets")
public class AssetsServiceImpl implements AssetsService {
    @Autowired
    private AssetsMapper assetsMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(AssetsInsertDTO assetsInsertDTO) {
        Assets assets = BeanUtil.copyProperties(assetsInsertDTO, Assets.class);


        assets.setId(null);
        assets.setDeleted(0);
        assets.setCreated(LocalDateTime.now());
        assets.setUpdated(LocalDateTime.now());
        int result = assetsMapper.insert(assets);
        if (result <= 0) {
            throw new RuntimeException("DB添加失败");

        }

        return result;
    }

    @Cacheable(key = "#id", condition = "#id !=null", unless = " #result==null")
    @Override
    public Assets selectById(Long id) {
        Assets result = assetsMapper.selectById(id);
        if (ObjectUtil.isNull(result)) {
            throw new ServiceException("ID不存在");
        }
        return result;
    }
@Transactional(rollbackFor = Exception.class)
    @CacheEvict(allEntries = true)
    @Override
    public int updateById(AssetsUpdateDTO assetsUpdateDTO) {
        Assets assets = BeanUtil.copyProperties(assetsUpdateDTO, Assets.class);

        assets.setCreated(null);
        assets.setUpdated(LocalDateTime.now());
        int result = assetsMapper.updateById(assets);
        if (result <= 0) {
            throw new ServiceException("DB修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {

        int result = assetsMapper.logicDeleteById(id);
        if (result <= 0) {
            throw new ServiceException("DB逻辑删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {

        int result = assetsMapper.logicDeleteByIds(ids);
        if (result <= 0) {
            throw new ServiceException("DB逻辑批删失败");

        }
        return result;
    }


@Cacheable(key = "#root.methodName",unless = "#result==null")
    @Override
    public List<Assets> list() {
        return assetsMapper.list();
    }
@Cacheable(key = "#root.methodName + ':' + " +
"#pageDTO.getPageNum()+':' + " +
"#pageDTO.getPageSize()",unless = "#result == null ")
    @Override
    public PageInfo<Assets> page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());

        List<Assets> list = assetsMapper.list();
        PageInfo<Assets> pageInfo = new PageInfo(list);

        return pageInfo;
    }

@Cacheable(key = "#root.methodName + ':' + " +
        "#dto.getTitle() + ':' + " +
        "#dto.getPageNum() + ':' +" +
        "#dto.getPageSize()",unless = "#result==null")
    @Override
    public PageInfo<Assets> pageLikeTitle(AssetsPageDTO dto) {
    PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
    return new PageInfo<>(assetsMapper.listLikeTitle(dto.getTitle()));
}
}
